<?php

/* 
 * Connection class
 * Author: Sergi Juanola korcholis [at] gmail [dot] com
 * Lets you connect to an SQL database using an XML config file and
 * passes its result to an array.
 */ 

require_once('XMLParser.php');

class Connection
{
    private $configFile = "./model/config/sqlconn.xml";
    private $cnx;

    public function __construct()
    {
        $xml = new XMLParser($this->configFile);
        $values = array("hostname"=>"","username"=>"","password"=>"","database"=>"");
        $values = $xml->getValue($values);
        $this->cnx = mysql_connect($values['hostname'], $values['username'], $values['password']) or trigger_error(mysql_error(),E_USER_ERROR);
        mysql_select_db($values['database'], $this->cnx);
    }
    
    public function __destruct()
    {
        mysql_close($this->cnx);
    }
    
    public function getResults($query)
    {
        $result = mysql_query($query, $this->cnx);
        $n = 0;
        while (FALSE !== ($row = mysql_fetch_assoc($result)))
        {
            foreach ($row as $key => $value)
            {
                $array[$n][$key] = $value;
            }
            $n++;
        }
        return $array;
	}
}
?>
